package com.zx.controller;

import com.zx.pojo.SmbmsProvider;
import com.zx.service.ProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/provider/")
public class ProviderController {
    @Autowired
    private ProviderService providerService;


    @RequestMapping("list")
    public String list(Model model) {
        List<SmbmsProvider> uproviderlist=providerService.findAllByProvider();
        model.addAttribute("uproviderlist",uproviderlist);
        //控制器层调用service层
       return "providerlist";
    }

    @RequestMapping("goupdate")
    public String goupdate(Long uid,Model model){
        SmbmsProvider smbmsProvider=providerService.findProviderById(uid);
        model.addAttribute("eprovider",smbmsProvider);
        return "providermodify";
    }

    @RequestMapping("doupdate")
    public String doupdate(SmbmsProvider smbmsProvider,Model model){
        boolean flag=providerService.updateProvider(smbmsProvider);
        if (flag){
            return "forward:list";
        }else {
            model.addAttribute("msg","修改失败！");
            return "providerlist";
        }
    }

    @RequestMapping("details/{id}")
    public String details(@PathVariable("id") Long uid, Model model){
        SmbmsProvider smbmsProvider=providerService.findProviderById(uid);
        model.addAttribute("dprovider",smbmsProvider);
        return "providerview";
    }

    @RequestMapping("del")
    public String del( Long uid, Model model){
        boolean flag=providerService.delProvider(uid);
        if (flag){
            return "forward:list";
        }else {
            model.addAttribute("msg","删除失败！");
            return "providerlist";
        }
    }

    @RequestMapping("save")
    public String save(SmbmsProvider smbmsProvider,Model model){
        boolean flag=providerService.addProvider(smbmsProvider);
        if (flag){
            return "forward:list";
        }else {
            model.addAttribute("msg","增加失败！");
            return "providerlist";
        }
    }
}
